import { AccountRoles } from './accountRoles';
import { Define, EntityObjectFactory } from 'tiny-entity2';
const EntityObjectType = EntityObjectFactory.GetEntityObjectType(process.env.dataBaseType || 'mysql');
import { ImageStore } from './imageStore';
import { Account } from './account';

@Define.Table({ TableName: "user" })
export class User extends EntityObjectType<User>{
    @Define.PrimaryKey()
    id: string;

    @Define.Column({ DataType: Define.DataType.VARCHAR, NotAllowNULL: true })
    nickName: string;

    @Define.Column({ DataType: Define.DataType.VARCHAR, NotAllowNULL: true })
    phone: string;

    @Define.Column({ DataType: Define.DataType.INT, DataLength: 11 })
    gender: number;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        IsIndex: true,
        NotAllowNULL: true
    })
    accountId: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true,
        IsIndex: true
    })
    storeId: string;

    @Define.Column({
        DataType: Define.DataType.INT,
        DataLength: 2,
        DefaultValue: 0
    })
    status: number;

    @Define.Mapping({
        Mapping: 'Account',
        MappingKey: { FKey: 'id', MKey: 'accountId' },
        MappingType: Define.MappingType.One
    })
    account: Account;

    @Define.Column({
        DataType: Define.DataType.BIGINT,
        NotAllowNULL: true
    })
    createTime: number;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    type: string;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    version: number;

    @Define.Mapping({
        Mapping: 'ImageStore',
        MappingKey: { FKey: 'objectId', MKey: 'id' }
    })
    images: ImageStore[];

    @Define.Mapping({
        Mapping: 'AccountRoles',
        MappingKey: { FKey: 'accountId', MKey: 'accountId' }
    })
    accountRoles: AccountRoles[]
}

export class UserStatus {
    constructor(
        private name: string,
        private value: number) { }

    get Name() { return this.name; }
    get Value() { return this.value; }

    static Prase(value: number) {
        switch (value) {
            case 0: return UserStatus.Normal;
            case 1: return UserStatus.Disable;
            default: throw new Error('not allowed UserStatus value.');
        }
    }

    static Normal = new UserStatus('normal', 0);
    static Disable = new UserStatus('disable', 1);
}

export const UserType = {
    Employee: 'employee',
    Owner: 'owner'
}